package com.classic.base;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.support.annotation.DrawableRes;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.classic.content.contents;
import com.classic.duanzhi.R;
import com.classic.utils.UtilsPrivacyPolicy;
import com.qy.sdk.Interfaces.ISDKinitialize;
import com.qy.sdk.Interfaces.RDInterface;
import com.qy.sdk.RDSDK;
import com.qy.sdk.Utils.ErrorCode;
import com.qy.sdk.rds.SplashView;


public abstract class UBaseQYSplashActivity extends UBaseActivity {
    ImageView img;
    UtilsPrivacyPolicy u_utils;

    protected abstract @DrawableRes
    int getImg();

    @Override
    protected int getResourceId() {
        return R.layout.activity_splash_u;
    }

    /**
     * 只用于初始化当前app需要实例化的内容  不适用当前页面初始化数据
     */
    protected abstract void initData();

    public void initUContent() {

    }

    @Override
    protected void initView() {
        initUContent();
        img = findViewById(R.id.v_iv_img);
        u_utils = findViewById(R.id.u_utils);
        ImageView ivLogo = findViewById(R.id.iv_logo);
        Glide.with(UBaseQYSplashActivity.this).load(getImg()).into(img);
        Glide.with(UBaseQYSplashActivity.this).load(getImgLogo()).into(ivLogo);

        initData();
        u_utils.setOnUtilsPrivacyPolicyListener(new UtilsPrivacyPolicy.OnUtilsPrivacyPolicyListener() {
            @Override
            public void onPermission() {//允许进入 进行操作
                initADType(1);
            }
        });
    }
    protected abstract String getChannel();

    /**
     * 请求广告
     *
     * @param index 0 无广告 1 有广告
     */
    public void initADType(int index) {
        if (index == 0) {
            starMain(1);
        } else {
            RDSDK.create().setActivity(this)
                    .setAppId(contents.QY_ID)
                    .setChannel(getChannel())//可为空或不填
                    .setHandler(new Handler())//handler需要在UI主线程中创建
                    .setSDKInitialize(new ISDKinitialize() {
                        @Override
                        public void initSucceed(RDSDK sdk) {
//SDK初始化成功,这之后才能展示广告，否则广告可能无法展现
                            System.out.println("SDK初始化完成");
//初始化成功后才能调用广告API，否则会空指针错误
                            final SplashView ad = new SplashView();
//                ad.setIntent(new Intent(UBaseSplashActivity.this,MainActivity.class));
                            ad.setInterface(UBaseQYSplashActivity.this, new RDInterface() {
                                @Override
                                public void onShow() {
                                    super.onShow();
                                    Log.i(" ----YZX-----", "广告展示");
                                }

                                @Override
                                public void onClose() {
                                    super.onClose();
                                    Log.i(" ----YZX-----", "广告关闭");
                                    starMain(10025);
                                }

                                @Override
                                public void onClick() {
                                    super.onClick();
                                    Log.i(" ----YZX-----", "广告点击");
                                    starMain(10024);
                                }

                                @Override
                                public void onError(String string) {
                                    super.onError(string);
                                    Log.i(" ----YZX-----", "广告异常" + string);
                                    starMain(10023);
                                }

                                @Override
                                public void onLoadSuccess() {
                                    super.onLoadSuccess();
                                    Log.i(" ----YZX-----", "广告准备状态" + ad.isReady());
                                    if (ad.isReady()) {
                                        ad.show();
                                    } else {
                                        ad.load();
                                    }
                                }
                            });
                            ad.load();
                        }

                        @Override
                        public void initError(ErrorCode errorCode) {
                            //SDK初始化错误日志
                            Log.i(" ----YZX-----", "SDK初始化错误日志" + errorCode.getMsg());
//
                            if(errorCode==ErrorCode.NOT_PERMISSION){//初始化原因为没有权限
                                RDSDK sdk=RDSDK.getSdk();
                                if(sdk!=null&&!sdk.checkSelfPermission()) {
                                    sdk.requestPermissions();
                                }else if(sdk!=null){
                                }else{
                                }
//                                RDSDK.getSdk().requestPermissions();
                            }
                        }

                    }).build();


        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        RDSDK sdk = RDSDK.getSdk();
        if (sdk != null)
            sdk.onResume();

    }

    @Override
    protected void onPause() {
        super.onPause();
        RDSDK sdk = RDSDK.getSdk();
        if (sdk != null)
            sdk.onPause();

    }

    /**
     * 跳转到主页
     *
     * @param i 当前跳转值
     */
    public abstract void starMain(int i);



    /**
     * @return 启动页显示图片
     */
    protected abstract @DrawableRes
    int getImgLogo();


    /**
     * @param height
     * @return 动态计算logo高度 适配屏幕
     */
    public abstract double getImgLogoScale(int height);
}
